<template>
  <div>
    <el-card class="box-card">
      <PageTools :totalNum="total">
        <!-- 新增按钮 -->
          <template #buttonPart>
            <el-row>
              <el-col :span="24">
                <el-button type="success" icon="el-icon-edit" size="medium" @click="$router.push('/questionbank/TestEntry')">新增文章</el-button>
              </el-col>
          </el-row>
          </template>
        <!-- 表单 -->
        <template #formPart>
          <searchQuest @onSearch="onSearch"></searchQuest>
        </template>
      </PageTools>

      <!-- 标签页 -->

      <!-- 表格：展示数据 -->
      <div class="table-content">
        <el-tabs type="card" @tab-click="handleClick" v-model="checkState" style="margin-top: 10px;">
          <el-tab-pane label="全部" name="all">
          </el-tab-pane>
          <el-tab-pane label="待审核" name="notcheck">
          </el-tab-pane>
          <el-tab-pane label="已审核" name="checked">
          </el-tab-pane>
          <el-tab-pane label="已拒绝" name="refused">
          </el-tab-pane>
        </el-tabs>
        <ListTable
          :loading="loading"
          :listData="listData"
          @openDialog="updateState"
          @success="handleClick"
          @previewQuest="previewQuest"></ListTable>
      </div>

      <!-- 分页 -->
      <div class="page-divided">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[5, 10, 20, 50]"
          :page-size="params.pagesize"
          layout="sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </el-card>

    <CheckDialog
      :isVisible.sync="isVisible"
      :passId="passId"
      @success="getList(params)"
    ></CheckDialog>

    <PreviewDialog
      :visible.sync="visible"
      :passId="passId"
    ></PreviewDialog>
  </div>
</template>

<script>
import { getSelectedList } from '@/api/querstionbank'
import PageTools from '../components/pageTools.vue'
import searchQuest from '../components/searchQuest.vue'
import CheckDialog from './components/checkDialog.vue'
import ListTable from './components/listTable.vue'
import PreviewDialog from '../components/previewDialog.vue'
export default {
  name: 'SelectedQuestionBank',
  components: {
    PageTools,
    searchQuest,
    CheckDialog,
    ListTable,
    PreviewDialog
},
  data() {
    return {
      params: {
        page: 1,
        pagesize: 10
      },
      currentPage: 1,
      listData: [],
      total: 0,
      visible: false,
      isVisible: false,
      passId: null,
      isAdd: true,
      checkState: 'all',
      loading: false
    }
  },

  methods: {
    // 获取全部文章列表
    async getList(data) {
      this.loading = true
      const res = await getSelectedList(data)
      this.total = res.counts
      this.listData = res.items
      this.loading = false
    },
    // 处理分页
    handleSizeChange(val) {
      this.params.pagesize = val
      this.getList(this.params)
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.params.page = val
      this.getList(this.params)
    },

    onSearch(obj){
      if(obj){
        this.getList({...this.params, ...obj})
      }else {
        this.getList(this.params)
      }
    },
    async updateState(id) {
      this.isVisible = true
      this.passId = id
    },
    // tab栏切换
    handleClick() {
      console.log(this.checkState)
      if (this.checkState==='all'){
        this.getList(this.params)
      }
      if (this.checkState === 'notcheck'){
        this.getList({
          chkState: 0,
          ...this.params
        })
      }
      if (this.checkState === 'checked') {
        this.getList({
          chkState: 1,
          ...this.params
        })
      }

      if (this.checkState === 'refused'){
        this.getList({
          chkState: 2,
          ...this.params
        })
      }
    },
    previewQuest(id) {
      this.visible = true
      this.passId = id
    }
  },

  created() {
    this.getList(this.params)
  }
}
</script>

<style lang="scss" scoped>
.page-divided{
  display: flex;
  justify-content: flex-end;
  margin-top: 20px;
}
// .table-content{
//   margin-top: 20px;
// }

</style>
